{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 109,
   "source": [
    "import numpy as np\r\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "source": [
    "data = pd.read_csv(r'D:/DeskTop-D/数据分析学习/数据分析代码/数据分析代码/03pandas源码及文件/pandas源码及文件/源码/作业参考答案/guazi.csv')\r\n",
    "data.head(15)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>leixing</th>\n",
       "      <th>nianfen</th>\n",
       "      <th>licheng</th>\n",
       "      <th>didian</th>\n",
       "      <th>shoujia</th>\n",
       "      <th>yuanjia</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>别克 君威 2014款 GS 2.0T 燃情运动版</td>\n",
       "      <td>2015年</td>\n",
       "      <td>3.0万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>补贴后11.97万</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      leixing  nianfen  licheng  didian    shoujia  yuanjia\n",
       "0     凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙     16.77万   34.60万\n",
       "1        奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙     21.96万   44.50万\n",
       "2      本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙      8.87万   15.20万\n",
       "3      大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙      7.27万   14.90万\n",
       "4                     leixing  nianfen  licheng  didian    shoujia  yuanjia\n",
       "5     凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙     16.77万   34.60万\n",
       "6        奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙     21.96万   44.50万\n",
       "7      本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙      8.87万   15.20万\n",
       "8      大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙      7.27万   14.90万\n",
       "9                     leixing  nianfen  licheng  didian    shoujia  yuanjia\n",
       "10    凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙     16.77万   34.60万\n",
       "11       奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙     21.96万   44.50万\n",
       "12     本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙      8.87万   15.20万\n",
       "13     大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙      7.27万   14.90万\n",
       "14  别克 君威 2014款 GS 2.0T 燃情运动版    2015年   3.0万公里      长沙  补贴后11.97万      NaN"
      ]
     },
     "metadata": {},
     "execution_count": 110
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "缺失值处理"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "source": [
    "# 首先查找缺失值\r\n",
    "data.isnull()\r\n",
    "# dropna() 删除缺失值 会丢失一整行\r\n",
    "# fillna() 填充缺失值\r\n",
    "data.fillna(0) # 把nan处全部换成0\r\n",
    "# fillna({1:0,2:3}) 把列索引为1的缺失处换成0  把..2处换成3  inplace=True就地修改\r\n",
    "# fillna(method='ffill') 将缺失值等于上面紧挨的非缺失值\r\n",
    "data.dropna()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>leixing</th>\n",
       "      <th>nianfen</th>\n",
       "      <th>licheng</th>\n",
       "      <th>didian</th>\n",
       "      <th>shoujia</th>\n",
       "      <th>yuanjia</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>大众 途观 2013款 1.8TSI 自动两驱舒适版</td>\n",
       "      <td>2014年</td>\n",
       "      <td>7.3万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>13.50万</td>\n",
       "      <td>25.80万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>现代ix35 2012款 2.0L 自动两驱精英版GLS</td>\n",
       "      <td>2012年</td>\n",
       "      <td>7.1万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.00万</td>\n",
       "      <td>21.30万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>宝马3系 2014款 320Li 时尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>4.6万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23.00万</td>\n",
       "      <td>38.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>标致308 2014款 乐享版 经典 1.6L 手动优尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>3.0万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>6.20万</td>\n",
       "      <td>11.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>大众POLO 2014款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.9万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.40万</td>\n",
       "      <td>11.30万</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1937 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            leixing  nianfen  licheng  didian  shoujia  \\\n",
       "0           凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙   16.77万   \n",
       "1              奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙   21.96万   \n",
       "2            本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙    8.87万   \n",
       "3            大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙    7.27万   \n",
       "4                           leixing  nianfen  licheng  didian  shoujia   \n",
       "...                             ...      ...      ...     ...      ...   \n",
       "2005     大众 途观 2013款 1.8TSI 自动两驱舒适版    2014年   7.3万公里      长沙   13.50万   \n",
       "2006   现代ix35 2012款 2.0L 自动两驱精英版GLS    2012年   7.1万公里      长沙    8.00万   \n",
       "2007           宝马3系 2014款 320Li 时尚型    2015年   4.6万公里      长沙   23.00万   \n",
       "2008  标致308 2014款 乐享版 经典 1.6L 手动优尚型    2015年   3.0万公里      长沙    6.20万   \n",
       "2009        大众POLO 2014款 1.6L 自动舒适版    2016年   2.9万公里      长沙    7.40万   \n",
       "\n",
       "      yuanjia  \n",
       "0      34.60万  \n",
       "1      44.50万  \n",
       "2      15.20万  \n",
       "3      14.90万  \n",
       "4     yuanjia  \n",
       "...       ...  \n",
       "2005   25.80万  \n",
       "2006   21.30万  \n",
       "2007   38.90万  \n",
       "2008   11.50万  \n",
       "2009   11.30万  \n",
       "\n",
       "[1937 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 111
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "source": [
    "# 移除重复数据\r\n",
    "data.duplicated() # 表示每一行是否是重复是，前面如果有就是true\r\n",
    "d = data.drop_duplicates() # 删除重复行\r\n",
    "d\r\n",
    "#data['v1'] = \"a\"\r\n",
    "data\r\n",
    "#data.drop_duplicates(['v1']) # 按照v1这一列来去除重复"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>leixing</th>\n",
       "      <th>nianfen</th>\n",
       "      <th>licheng</th>\n",
       "      <th>didian</th>\n",
       "      <th>shoujia</th>\n",
       "      <th>yuanjia</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>大众 途观 2013款 1.8TSI 自动两驱舒适版</td>\n",
       "      <td>2014年</td>\n",
       "      <td>7.3万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>13.50万</td>\n",
       "      <td>25.80万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>现代ix35 2012款 2.0L 自动两驱精英版GLS</td>\n",
       "      <td>2012年</td>\n",
       "      <td>7.1万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.00万</td>\n",
       "      <td>21.30万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>宝马3系 2014款 320Li 时尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>4.6万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23.00万</td>\n",
       "      <td>38.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>标致308 2014款 乐享版 经典 1.6L 手动优尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>3.0万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>6.20万</td>\n",
       "      <td>11.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>大众POLO 2014款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.9万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.40万</td>\n",
       "      <td>11.30万</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2010 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            leixing  nianfen  licheng  didian  shoujia  \\\n",
       "0           凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙   16.77万   \n",
       "1              奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙   21.96万   \n",
       "2            本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙    8.87万   \n",
       "3            大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙    7.27万   \n",
       "4                           leixing  nianfen  licheng  didian  shoujia   \n",
       "...                             ...      ...      ...     ...      ...   \n",
       "2005     大众 途观 2013款 1.8TSI 自动两驱舒适版    2014年   7.3万公里      长沙   13.50万   \n",
       "2006   现代ix35 2012款 2.0L 自动两驱精英版GLS    2012年   7.1万公里      长沙    8.00万   \n",
       "2007           宝马3系 2014款 320Li 时尚型    2015年   4.6万公里      长沙   23.00万   \n",
       "2008  标致308 2014款 乐享版 经典 1.6L 手动优尚型    2015年   3.0万公里      长沙    6.20万   \n",
       "2009        大众POLO 2014款 1.6L 自动舒适版    2016年   2.9万公里      长沙    7.40万   \n",
       "\n",
       "      yuanjia  \n",
       "0      34.60万  \n",
       "1      44.50万  \n",
       "2      15.20万  \n",
       "3      14.90万  \n",
       "4     yuanjia  \n",
       "...       ...  \n",
       "2005   25.80万  \n",
       "2006   21.30万  \n",
       "2007   38.90万  \n",
       "2008   11.50万  \n",
       "2009   11.30万  \n",
       "\n",
       "[2010 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 112
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "source": [
    "#利用函数或映射进行数据转换\r\n",
    "# 映射\r\n",
    "d = pd.DataFrame({'food': ['Apple',\"tomato\",'Desk','Chair'],\r\n",
    "                'Price':['1','2','3','4']})\r\n",
    "d"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Apple</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tomato</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Desk</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Chair</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     food Price\n",
       "0   Apple     1\n",
       "1  tomato     2\n",
       "2    Desk     3\n",
       "3   Chair     4"
      ]
     },
     "metadata": {},
     "execution_count": 113
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "source": [
    "meat = {'apple': 'fruit', 'tomato': 'vagetable', 'desk': 'furniture','chair':'furniture'}\r\n",
    "d['type'] = d['food'].map(meat)\r\n",
    "d\r\n",
    "# low = data['food'].str.lower() 把food这一列全部变成了小写\r\n",
    "# 利用匿名函数\r\n",
    "d['class'] = d['food'].map(lambda x:meat[x.lower()])\r\n",
    "d"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>Price</th>\n",
       "      <th>type</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Apple</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tomato</td>\n",
       "      <td>2</td>\n",
       "      <td>vagetable</td>\n",
       "      <td>vagetable</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Desk</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>furniture</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Chair</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>furniture</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     food Price       type      class\n",
       "0   Apple     1        NaN      fruit\n",
       "1  tomato     2  vagetable  vagetable\n",
       "2    Desk     3        NaN  furniture\n",
       "3   Chair     4        NaN  furniture"
      ]
     },
     "metadata": {},
     "execution_count": 114
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "source": [
    "# 值替换\r\n",
    "data1 = pd.Series([1,-999,2,-1000,3])\r\n",
    "data"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>leixing</th>\n",
       "      <th>nianfen</th>\n",
       "      <th>licheng</th>\n",
       "      <th>didian</th>\n",
       "      <th>shoujia</th>\n",
       "      <th>yuanjia</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>大众 途观 2013款 1.8TSI 自动两驱舒适版</td>\n",
       "      <td>2014年</td>\n",
       "      <td>7.3万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>13.50万</td>\n",
       "      <td>25.80万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>现代ix35 2012款 2.0L 自动两驱精英版GLS</td>\n",
       "      <td>2012年</td>\n",
       "      <td>7.1万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.00万</td>\n",
       "      <td>21.30万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>宝马3系 2014款 320Li 时尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>4.6万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23.00万</td>\n",
       "      <td>38.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>标致308 2014款 乐享版 经典 1.6L 手动优尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>3.0万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>6.20万</td>\n",
       "      <td>11.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>大众POLO 2014款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.9万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.40万</td>\n",
       "      <td>11.30万</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2010 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            leixing  nianfen  licheng  didian  shoujia  \\\n",
       "0           凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙   16.77万   \n",
       "1              奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙   21.96万   \n",
       "2            本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙    8.87万   \n",
       "3            大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙    7.27万   \n",
       "4                           leixing  nianfen  licheng  didian  shoujia   \n",
       "...                             ...      ...      ...     ...      ...   \n",
       "2005     大众 途观 2013款 1.8TSI 自动两驱舒适版    2014年   7.3万公里      长沙   13.50万   \n",
       "2006   现代ix35 2012款 2.0L 自动两驱精英版GLS    2012年   7.1万公里      长沙    8.00万   \n",
       "2007           宝马3系 2014款 320Li 时尚型    2015年   4.6万公里      长沙   23.00万   \n",
       "2008  标致308 2014款 乐享版 经典 1.6L 手动优尚型    2015年   3.0万公里      长沙    6.20万   \n",
       "2009        大众POLO 2014款 1.6L 自动舒适版    2016年   2.9万公里      长沙    7.40万   \n",
       "\n",
       "      yuanjia  \n",
       "0      34.60万  \n",
       "1      44.50万  \n",
       "2      15.20万  \n",
       "3      14.90万  \n",
       "4     yuanjia  \n",
       "...       ...  \n",
       "2005   25.80万  \n",
       "2006   21.30万  \n",
       "2007   38.90万  \n",
       "2008   11.50万  \n",
       "2009   11.30万  \n",
       "\n",
       "[2010 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 115
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "source": [
    "data1.replace(-999,np.nan)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "0       1.0\n",
       "1       NaN\n",
       "2       2.0\n",
       "3   -1000.0\n",
       "4       3.0\n",
       "dtype: float64"
      ]
     },
     "metadata": {},
     "execution_count": 116
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "source": [
    "data.replace([-999,-1000],[1,2])\r\n",
    "data.replace({-999:np.nan})"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>leixing</th>\n",
       "      <th>nianfen</th>\n",
       "      <th>licheng</th>\n",
       "      <th>didian</th>\n",
       "      <th>shoujia</th>\n",
       "      <th>yuanjia</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>凯迪拉克ATS-L 2016款 28T 时尚型</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>16.77万</td>\n",
       "      <td>34.60万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>奥迪A6L 2014款 TFSI 标准型</td>\n",
       "      <td>2014年</td>\n",
       "      <td>13.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>21.96万</td>\n",
       "      <td>44.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>本田 思域 2016款 1.8L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>4.8万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.87万</td>\n",
       "      <td>15.20万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大众 朗逸 2015款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>10.5万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.27万</td>\n",
       "      <td>14.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>leixing</td>\n",
       "      <td>nianfen</td>\n",
       "      <td>licheng</td>\n",
       "      <td>didian</td>\n",
       "      <td>shoujia</td>\n",
       "      <td>yuanjia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>大众 途观 2013款 1.8TSI 自动两驱舒适版</td>\n",
       "      <td>2014年</td>\n",
       "      <td>7.3万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>13.50万</td>\n",
       "      <td>25.80万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>现代ix35 2012款 2.0L 自动两驱精英版GLS</td>\n",
       "      <td>2012年</td>\n",
       "      <td>7.1万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>8.00万</td>\n",
       "      <td>21.30万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>宝马3系 2014款 320Li 时尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>4.6万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>23.00万</td>\n",
       "      <td>38.90万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>标致308 2014款 乐享版 经典 1.6L 手动优尚型</td>\n",
       "      <td>2015年</td>\n",
       "      <td>3.0万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>6.20万</td>\n",
       "      <td>11.50万</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>大众POLO 2014款 1.6L 自动舒适版</td>\n",
       "      <td>2016年</td>\n",
       "      <td>2.9万公里</td>\n",
       "      <td>长沙</td>\n",
       "      <td>7.40万</td>\n",
       "      <td>11.30万</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2010 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            leixing  nianfen  licheng  didian  shoujia  \\\n",
       "0           凯迪拉克ATS-L 2016款 28T 时尚型    2016年   2.5万公里      长沙   16.77万   \n",
       "1              奥迪A6L 2014款 TFSI 标准型    2014年  13.8万公里      长沙   21.96万   \n",
       "2            本田 思域 2016款 1.8L 自动舒适版    2016年   4.8万公里      长沙    8.87万   \n",
       "3            大众 朗逸 2015款 1.6L 自动舒适版    2016年  10.5万公里      长沙    7.27万   \n",
       "4                           leixing  nianfen  licheng  didian  shoujia   \n",
       "...                             ...      ...      ...     ...      ...   \n",
       "2005     大众 途观 2013款 1.8TSI 自动两驱舒适版    2014年   7.3万公里      长沙   13.50万   \n",
       "2006   现代ix35 2012款 2.0L 自动两驱精英版GLS    2012年   7.1万公里      长沙    8.00万   \n",
       "2007           宝马3系 2014款 320Li 时尚型    2015年   4.6万公里      长沙   23.00万   \n",
       "2008  标致308 2014款 乐享版 经典 1.6L 手动优尚型    2015年   3.0万公里      长沙    6.20万   \n",
       "2009        大众POLO 2014款 1.6L 自动舒适版    2016年   2.9万公里      长沙    7.40万   \n",
       "\n",
       "      yuanjia  \n",
       "0      34.60万  \n",
       "1      44.50万  \n",
       "2      15.20万  \n",
       "3      14.90万  \n",
       "4     yuanjia  \n",
       "...       ...  \n",
       "2005   25.80万  \n",
       "2006   21.30万  \n",
       "2007   38.90万  \n",
       "2008   11.50万  \n",
       "2009   11.30万  \n",
       "\n",
       "[2010 rows x 6 columns]"
      ]
     },
     "metadata": {},
     "execution_count": 117
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "离散化和面元划分"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "source": [
    "ages = [20, 22, 24, 25, 27, 21, 37, 94, 19, 82]\r\n",
    "bins = [12, 25, 35, 69, 100]\r\n",
    "cats = pd.cut(ages, bins)\r\n",
    "cats"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[(12, 25], (12, 25], (12, 25], (12, 25], (25, 35], (12, 25], (35, 69], (69, 100], (12, 25], (69, 100]]\n",
       "Categories (4, interval[int64]): [(12, 25] < (25, 35] < (35, 69] < (69, 100]]"
      ]
     },
     "metadata": {},
     "execution_count": 118
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "source": [
    "cats.categories"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "IntervalIndex([(12, 25], (25, 35], (35, 69], (69, 100]],\n",
       "              closed='right',\n",
       "              dtype='interval[int64]')"
      ]
     },
     "metadata": {},
     "execution_count": 119
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "source": [
    "pd.value_counts(cats)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(12, 25]     6\n",
       "(69, 100]    2\n",
       "(35, 69]     1\n",
       "(25, 35]     1\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 120
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "source": [
    "names = ['青年', '年轻', '中年', '老年']\r\n",
    "pd.cut(ages, bins, labels=names)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[青年, 青年, 青年, 青年, 年轻, 青年, 中年, 老年, 青年, 老年]\n",
       "Categories (4, object): [青年 < 年轻 < 中年 < 老年]"
      ]
     },
     "metadata": {},
     "execution_count": 121
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "source": [
    "data1 = np.random.rand(20)\r\n",
    "data1"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "array([0.48196848, 0.22120142, 0.65178062, 0.44552229, 0.35546557,\n",
       "       0.53527766, 0.5239507 , 0.65475567, 0.16031171, 0.68918968,\n",
       "       0.83024932, 0.07877705, 0.20661123, 0.13198554, 0.03302654,\n",
       "       0.78662244, 0.85768437, 0.11617046, 0.56622974, 0.53793467])"
      ]
     },
     "metadata": {},
     "execution_count": 122
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "source": [
    "cat = pd.cut(data1, 4, precision=2)\r\n",
    "pd.value_counts(cat)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "(0.032, 0.24]    7\n",
       "(0.65, 0.86]     6\n",
       "(0.45, 0.65]     6\n",
       "(0.24, 0.45]     1\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 123
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "检测和过滤异常值"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "source": [
    "data1[np.abs(data1)>3] = 3 # 将data中所有绝对值大于3的都等于3 "
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "source": [
    "df = pd.DataFrame(np.arange(5 * 4).reshape((5, 4)))\r\n",
    "df"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "0   0   1   2   3\n",
       "1   4   5   6   7\n",
       "2   8   9  10  11\n",
       "3  12  13  14  15\n",
       "4  16  17  18  19"
      ]
     },
     "metadata": {},
     "execution_count": 125
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "source": [
    "san = np.random.permutation(5)\r\n",
    "san\r\n",
    "df.take(san) #按照san这个进行排列\r\n",
    "df.sample(3) #随机选三行"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1   2   3\n",
       "2  8  9  10  11\n",
       "1  4  5   6   7\n",
       "0  0  1   2   3"
      ]
     },
     "metadata": {},
     "execution_count": 126
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "字符串操作"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "source": [
    "val = 'a,b, c'\r\n",
    "val.split(',') # 按逗号分隔\r\n",
    "for x in val.split(','):\r\n",
    "    x.strip() # 去除空格包括换行符\r\n",
    "x = \"22\".join(\"223\") #join连接字符串\r\n",
    "x\r\n",
    "'c' in val\r\n",
    "val.index(',')\r\n",
    "val.replace(',', ':')"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'a:b: c'"
      ]
     },
     "metadata": {},
     "execution_count": 127
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "source": [
    "data1 = {\"a\": \"123213@qq.com\", \"b\":\"gfdsfqe@163.com\", \"c\":np.nan}\r\n",
    "data1 = pd.Series(data1)\r\n",
    "data1"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a      123213@qq.com\n",
       "b    gfdsfqe@163.com\n",
       "c                NaN\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 128
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "source": [
    "data1.str.split(\"@\") \r\n",
    "data1.str.findall(\"@\")\r\n",
    "data1.str[:5]"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "a    12321\n",
       "b    gfdsf\n",
       "c      NaN\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "execution_count": 129
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "source": [
    "data.head(10)\r\n",
    "\r\n",
    "data.drop([4], inplace=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "source": [
    "(data.isnull()).sum()\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "leixing     0\n",
       "nianfen     0\n",
       "licheng     0\n",
       "didian      0\n",
       "shoujia     0\n",
       "yuanjia    73\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 131
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "source": [
    "data.dropna(subset=['yuanjia'], inplace=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "source": [
    "(data.duplicated()).sum()\r\n",
    "data.drop_duplicates(inplace=True) # 删除重复数据\r\n",
    "len(data)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1928"
      ]
     },
     "metadata": {},
     "execution_count": 133
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "source": [
    "data['shoujia'] = data.shoujia.map(lambda x:x.replace('万','')).astype('float64')\r\n"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "ValueError",
     "evalue": "could not convert string to float: 'shoujia'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-134-9e38ee6f2ab8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'shoujia'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshoujia\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'万'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'float64'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m   5696\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5697\u001b[0m             \u001b[1;31m# else, only a single dtype is given\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 5698\u001b[1;33m             \u001b[0mnew_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   5699\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_data\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__finalize__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5700\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m    580\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    581\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbool\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mstr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"raise\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 582\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"astype\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0merrors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    583\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    584\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001b[0m in \u001b[0;36mapply\u001b[1;34m(self, f, filter, **kwargs)\u001b[0m\n\u001b[0;32m    440\u001b[0m                 \u001b[0mapplied\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    441\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 442\u001b[1;33m                 \u001b[0mapplied\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    443\u001b[0m             \u001b[0mresult_blocks\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_extend_blocks\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mapplied\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult_blocks\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    444\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\internals\\blocks.py\u001b[0m in \u001b[0;36mastype\u001b[1;34m(self, dtype, copy, errors)\u001b[0m\n\u001b[0;32m    623\u001b[0m             \u001b[0mvals1d\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    624\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 625\u001b[1;33m                 \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mastype_nansafe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvals1d\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    626\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mValueError\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    627\u001b[0m                 \u001b[1;31m# e.g. astype_nansafe can fail on object-dtype of strings\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\dtypes\\cast.py\u001b[0m in \u001b[0;36mastype_nansafe\u001b[1;34m(arr, dtype, copy, skipna)\u001b[0m\n\u001b[0;32m    895\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mcopy\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mis_object_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mis_object_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    896\u001b[0m         \u001b[1;31m# Explicit copy, or required since NumPy can't view from / to object.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 897\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    898\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    899\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mview\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'shoujia'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "type(data['shoujia'][0])"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "KeyError",
     "evalue": "'shoujia'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_value\u001b[1;34m(self, series, key)\u001b[0m\n\u001b[0;32m   4410\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4411\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mlibindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value_at\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4412\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.get_value_at\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.get_value_at\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\util.pxd\u001b[0m in \u001b[0;36mpandas._libs.util.get_value_at\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\util.pxd\u001b[0m in \u001b[0;36mpandas._libs.util.validate_indexer\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'str' object cannot be interpreted as an integer",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-db2968bb23b4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'shoujia'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m    869\u001b[0m         \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    870\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 871\u001b[1;33m             \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    872\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    873\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_value\u001b[1;34m(self, series, key)\u001b[0m\n\u001b[0;32m   4417\u001b[0m                     \u001b[1;32mraise\u001b[0m \u001b[0mInvalidIndexError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4418\u001b[0m                 \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4419\u001b[1;33m                     \u001b[1;32mraise\u001b[0m \u001b[0me1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4420\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4421\u001b[0m                 \u001b[1;32mraise\u001b[0m \u001b[0me1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_value\u001b[1;34m(self, series, key)\u001b[0m\n\u001b[0;32m   4403\u001b[0m         \u001b[0mk\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_convert_scalar_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkind\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"getitem\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4404\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 4405\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtz\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseries\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"tz\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   4406\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   4407\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mholds_integer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_boolean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'shoujia'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "source": [
    "left = pd.DataFrame({'key': ['K0', 'k1', 'K2', 'K3'],\r\n",
    "                     'A': ['A0', 'A1', 'A2', 'A3'],\r\n",
    "                     'B': ['B0', 'B1', 'B2', 'B3']})\r\n",
    "right = pd.DataFrame({'key': ['K0', 'k1', 'K2', 'K3'],\r\n",
    "                     'C': ['C0', 'C1', 'C2', 'C3'],\r\n",
    "                     'D': ['D0', 'D1', 'D2', 'D3']})\r\n",
    "left"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>K0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>K2</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>K3</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key   A   B\n",
       "0  K0  A0  B0\n",
       "1  k1  A1  B1\n",
       "2  K2  A2  B2\n",
       "3  K3  A3  B3"
      ]
     },
     "metadata": {},
     "execution_count": 137
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "source": [
    "# 数据连接\r\n",
    "pd.merge(left,right)"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>K0</td>\n",
       "      <td>A0</td>\n",
       "      <td>B0</td>\n",
       "      <td>C0</td>\n",
       "      <td>D0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>k1</td>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>K2</td>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>K3</td>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key   A   B   C   D\n",
       "0  K0  A0  B0  C0  D0\n",
       "1  k1  A1  B1  C1  D1\n",
       "2  K2  A2  B2  C2  D2\n",
       "3  K3  A3  B3  C3  D3"
      ]
     },
     "metadata": {},
     "execution_count": 138
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "source": [
    "pd.merge(left, right, on='key')"
   ],
   "outputs": [
    {
     "output_type": "error",
     "ename": "KeyError",
     "evalue": "'A0'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-140-b00283dc6637>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmerge\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mleft\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mright\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'A0'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36mmerge\u001b[1;34m(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)\u001b[0m\n\u001b[0;32m     84\u001b[0m         \u001b[0mcopy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     85\u001b[0m         \u001b[0mindicator\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindicator\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 86\u001b[1;33m         \u001b[0mvalidate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mvalidate\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     87\u001b[0m     )\n\u001b[0;32m     88\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_result\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)\u001b[0m\n\u001b[0;32m    625\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mright_join_keys\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    626\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin_names\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 627\u001b[1;33m         ) = self._get_merge_keys()\n\u001b[0m\u001b[0;32m    628\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    629\u001b[0m         \u001b[1;31m# validate the merge keys dtypes. We may need to coerce\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36m_get_merge_keys\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    981\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_rkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    982\u001b[0m                         \u001b[1;32mif\u001b[0m \u001b[0mrk\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 983\u001b[1;33m                             \u001b[0mright_keys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mright\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_label_or_level_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    984\u001b[0m                         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    985\u001b[0m                             \u001b[1;31m# work-around for merge_asof(right_index=True)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m_get_label_or_level_values\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1690\u001b[0m             \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxes\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_level_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1691\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1692\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1693\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1694\u001b[0m         \u001b[1;31m# Check for duplicates\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'A0'"
     ]
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.7.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.6 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "af997f310b38b8fff913e3035512343e167e2ca0aa226bb6d1eaa902db614b07"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}